package com.javaProject.controller;


import com.javaProject.dto.R;
import com.javaProject.entity.Admin;
import com.javaProject.entity.Student;
import com.javaProject.entity.Teacher;
import com.javaProject.service.AdminService;
import com.javaProject.service.StudentService;

import com.javaProject.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

@CrossOrigin
@RestController
@Tag(name="管理员接口",description = "这个接口的目的主要用于登陆和设置时间")
public class adminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;



    @GetMapping("/admin")
    @Operation(summary = "登陆",description = "提供账号密码")
    @Parameters({
            @Parameter(name = "username",description = "账号"),
            @Parameter(name = "password",description = "密码")
    })
    public R login(String username,String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
        Student student = new Student();
        student.setUsername(username);
        student.setPassword(password);

        Teacher teacher = new Teacher();
        teacher.setUsername(username);
        teacher.setPassword(password);


        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        if (adminService.login(admin).getCode() == 0)
            return adminService.login(admin).setCode(2);
        else if (teacherService.login(teacher).getCode()==0)
            return teacherService.login(teacher).setCode(1);
        else
            return studentService.login(student);
    }

}
